package com.zf.wms.purview.service.Ipml;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zf.wms.purview.dao.RoleMapper;
import com.zf.wms.purview.dao.SynthesizeMapper;
import com.zf.wms.purview.entity.Role;
import com.zf.wms.purview.entity.Synthesize;
import com.zf.wms.purview.exception.OperateException;
import com.zf.wms.purview.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * Author：sl
 * Date：2019-11-04 10:07
 * Description：<描述>
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {


    @Autowired
    RoleMapper roleMapper;

    @Autowired
    SynthesizeMapper synthesizeMapper;


    /**
     * 删除角色
     *
     * @param roleId
     */
    public void deleteById(Long roleId) throws OperateException {

        //删除角色之前先查询该角色是否绑定了用户，绑定用户的角色不可以被删除

        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("department_role_id",roleId);
        List list = synthesizeMapper.selectList(queryWrapper);

        //判断根据roleId查询到的中间表对象是否为空，空则没有绑定可以删除，不为空则抛出异常  不可删除
        if (0!=list.size()) {
            throw new OperateException("此角色已被绑定，无法删除！");
        }else {
            roleMapper.deleteById(roleId);
        }


    }


}
